package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.ProductSpecService;
import com.atguigu.spzx.model.pojo.product.ProductSpec;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.utils.DYLAssert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dyl
 * @version 1.0
 * @description:
 * @date 2025/1/8 17:22
 */
@RestController
@RequestMapping("/admin/product/productSpec")
@Tag(name = "商品规格管理")
@CrossOrigin(allowCredentials = "true", originPatterns = "*", allowedHeaders = "*")
public class ProductSpecController {
    @Autowired
    private ProductSpecService productSpecService;


    @GetMapping("/findPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询")
    public Result<Page<ProductSpec>> findPage(@PathVariable("pageNum") Integer pageNum,
                                              @PathVariable("pageSize") Integer pageSize,
                                              @RequestParam("searchName") String searchName){
        return Result.ok(productSpecService.findPage(pageNum,pageSize,searchName));
    }


    @PostMapping("/addProductSpec")
    @Operation(summary = "添加商品规格")
    public Result addProductSpec(@RequestBody ProductSpec productSpec){
        DYLAssert.isTrue(StringUtils.isNotEmpty(productSpec.getSpecName()),"添加失败");
        productSpecService.save(productSpec);
        return Result.ok();
    }

    @PutMapping("/updateProductSpec")
    @Operation(summary = "修改商品规格")
    public Result updateProductSpec(@RequestBody ProductSpec productSpec){
        DYLAssert.isTrue(StringUtils.isNotEmpty(productSpec.getSpecName()),"添加失败");
        productSpecService.updateById(productSpec);
        return Result.ok();
    }

    @DeleteMapping("/deleteProductSpec/{id}")
    @Operation(summary = "修改商品规格")
    public Result updateProductSpec(@PathVariable("id")Long id){
        DYLAssert.notNull(id,"添加失败");
        productSpecService.removeById(id);
        return Result.ok();
    }

    @GetMapping("/findAll")
    @Operation(summary = "获取分类列表")
    public Result<List<ProductSpec>> findAll(){
        return Result.ok(productSpecService.list());
    }

}
